package com.njlime.cms.repository.sys;

import com.njlime.cms.entities.MaterialsVoice;
import com.njlime.cms.entities.PublishReport;
import com.njlime.core.base.repository.impl.BaseDaoImpl;
import com.njlime.core.helper.PageBean;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

@Repository
public class MaterialVoiceDaoImpl extends BaseDaoImpl<MaterialsVoice>{

	public MaterialsVoice get(int id, int unitId){
        StringBuffer sb = new StringBuffer();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("select * from t_materials_voice where id = ? and unit_id = ? and del = 0");
        arrList.add(id);
        arrList.add(unitId);
        return findOne(sb.toString(),arrList);
	}

    /**
     * 图片查询
     * @param pageBean
     * @return
     */
	public PageBean getImageList(PageBean pageBean,  int unitId,int groupId,String name){
        StringBuffer sb = new StringBuffer();
        List<Object> arrList = new ArrayList<Object>();

        sb.append("select id, image_path ,voice_path,voice_time_length,title ");
		sb.append("from t_materials_voice t  ");

		sb.append("where t.unit_id = ? and del = 0 ");
		arrList.add(unitId);
		if(groupId >0){
			sb.append(" and voice_group_id = ? ");
			arrList.add(groupId);
		}
		if( StringUtils.isNotEmpty(name)){
			sb.append(" and t.title like ? ");
			arrList.add("%"+name+"%");
		}
		int num = getListJdbcSize(sb.toString(),arrList);
		pageBean.setTotalCount(num);
		if (num > 0) {
			sb.append(" order by id desc limit ?,?");
			arrList.add((pageBean.getCurPage() - 1) * pageBean.getPageSize());
			arrList.add(pageBean.getPageSize());
			pageBean.setMapList(getListJdbc(sb.toString(), arrList));
		}
		return pageBean;
	}


	/**
	 *
	 *
	 * @param id
	 * @param unitId
	 * @return
	 */
	public void delImage(int id, int unitId){
		StringBuilder sb = new StringBuilder();
		List<Object> arrList = new ArrayList<Object>();
		sb.append("UPDATE t_materials_voice set del = 1");

		sb.append(" where id = ? and unit_id = ?");
		arrList.add(id);
		arrList.add(unitId);
		updateJdbc(sb.toString(), arrList);
	}

	/**
	 * 图片分组
	 * @param id 图片id
	 * @param groupId 分组id
	 * @param unitId 单位id
	 */
	public void editImageGroup(int id, int groupId, int unitId){
		StringBuilder sb = new StringBuilder();
		List<Object> arrList = new ArrayList<Object>();
		sb.append("UPDATE t_materials_voice set voice_group_id =   ?");
		sb.append(" where id= ?  and unit_id = ?");

		arrList.add(groupId);
		arrList.add(id);
		arrList.add(unitId);
		updateJdbc(sb.toString(), arrList);
	}
}
